package com.ljfiction.book.activitys;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ljfiction.book.Baner.Bookchapters;
import com.ljfiction.book.Baner.ShuqianBean;
import com.ljfiction.book.R;
import com.ljfiction.book.adapter.IndentDetailAdapter;
import com.ljfiction.book.fragment.ReadReconntBookdatas;
import com.ljfiction.book.fragment.ShuQianBookdatas;
import com.ljfiction.book.http.HttpManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class ContentsActivity extends BaseActivity {

    @BindView(R.id.contents_xtablayout)
    XTabLayout contentsXtablayout;
    @BindView(R.id.contents_viewpager)
    ViewPager contentsViewpager;
    @BindView(R.id.back_contents)
    ImageView backContents;
    @BindView(R.id.bookname_contents)
    TextView booknameContents;
    @BindView(R.id.top_contents)
    RelativeLayout topContents;

    private ReadReconntBookdatas mulu;
    private ShuQianBookdatas shuqian;
    private List<ShuqianBean> listshuqian = new ArrayList<ShuqianBean>();
    private String tabTitles[] = new String[]{"目录", "书签"};
    private String bookId;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private List<Bookchapters.DataBean.ChapterInfoBean> mContentChapterList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contents);
        ButterKnife.bind(this);
        bookId = getIntent().getStringExtra("bookId");
        booknameContents.setText(getIntent().getStringExtra("bookName"));
        topContents.getBackground().setAlpha(255);
        initData();

    }

    private void initData() {
        OkHttpUtils.get(HttpManager.IP + "/books-volumeChapterList/" + bookId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        try {
                            Bookchapters bookchapters = gson.fromJson(s, Bookchapters.class);
                            if (200 == bookchapters.getReturnCode()) {
                                mContentChapterList = bookchapters.getData().getChapterInfo();
                                inttoview();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void inttoview() {
        mulu = ReadReconntBookdatas.newInstance(this, mContentChapterList, bookId);
        shuqian = ShuQianBookdatas.newInstance(this, bookId);
        fragments.add(mulu);
        fragments.add(shuqian);
        IndentDetailAdapter adapter = new IndentDetailAdapter(getSupportFragmentManager(), tabTitles, fragments);
        contentsViewpager.setAdapter(adapter);
        contentsViewpager.setOverScrollMode(View.OVER_SCROLL_NEVER);
        contentsXtablayout.setupWithViewPager(contentsViewpager);
    }

    @OnClick(R.id.back_contents)
    public void onViewClicked() {
        goBack();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (shuqian!=null){
            shuqian.gengxin();
        }
    }
}
